<template>
    <a-modal :confirm-loading="modalOption.loading" :okText="modalOption.okText" @ok="modalOption.onOk"
             @cancel="modalOption.onCancel" :cancelText="modalOption.cancelText" :width="modalOption.width" :visible="visible"
             :title="modalOption.title">
       <a-spin tip="Loading..." :spinning="loading">
           <div class="container">
               <a-form-model ref="formModel" :label-col="formOption.labelCol" :wrapper-col="formOption.wrapperCol"
                             :layout="formOption.layout" :label-align="formOption.labelAlign" :hide-required-mark="formOption.hideRequiredMark"
                             :rules="formOption.rules" :model="formOption.formData">
                   <div class="table">
                       <a-card :title="cardTitle1" hoverable>
                           <div class="tool-bar">
                               <a-row>
                                   <a-col :span="6">
                                       <a-form-model-item label="收入日期" prop="earnDate">
                                           <a-date-picker
                                               class="inputs"
                                               allowClear
                                               v-model="formOption.formData.earnDate"
                                               valueFormat="YYYY-MM-DD"
                                               placeholder="收入日期"
                                               :locale="locale" />
                                       </a-form-model-item>
                                   </a-col>
                                   <a-col :span="6">
                                       <a-form-model-item label="标准类型" prop="categoryCode">
                                           <a-select class="inputs" allowClear v-model="formOption.formData.categoryCode" placeholder="标准类型">
                                               <a-select-option
                                                   v-for="(item,i) in ['国标','省标','省标-北京','省标-上海']"
                                                   :key="i"
                                                   :value="item">{{item}}</a-select-option>
                                           </a-select>
                                       </a-form-model-item>
                                   </a-col>
                                   <a-col :span="6">
                                       <a-form-model-item label="品名" prop="materialName">
                                           <a-select :disabled="disabledEdit" @change="materialIdChange" class="inputs" allowClear v-model="formOption.formData.materialName" placeholder="品名">
                                               <a-select-option
                                                   v-for="(item,i) in searchSource.materialNameList"
                                                   :key="i"
                                                   :value="item.id">{{item.name}}</a-select-option>
                                           </a-select>
                                       </a-form-model-item>
                                   </a-col>
                                   <a-col :span="6">
                                       <a-form-model-item label="生产类型" prop="productionType">
                                           <a-select class="inputs" allowClear v-model="formOption.formData.productionType" placeholder="生产类型">
                                               <a-select-option
                                                   v-for="(item,i) in ['CW','SY','G']"
                                                   :key="i"
                                                   :value="item">{{item}}</a-select-option>
                                           </a-select>
                                       </a-form-model-item>
                                   </a-col>
                               </a-row>
                           </div>
                           <div class="tool-bar">
                               <a-row>
                                   <a-col :span="6">
                                       <a-form-model-item label="批号" prop="batch">
                                           <a-input class="inputs" allowClear v-model="formOption.formData.batch" placeholder="批号"></a-input>
                                       </a-form-model-item>
                                   </a-col>
                                   <a-col :span="6">
                                       <a-form-model-item label="规格" prop="spec">
                                           <a-input class="inputs" allowClear v-model="formOption.formData.spec" placeholder="规格"></a-input>
                                       </a-form-model-item>
                                   </a-col>
                                   <a-col :span="6">
                                       <a-form-model-item :label="tabName=='喷干-过筛'?'喷干重量(kg)':'混二重量(kg)'" prop="spec">
                                           <a-input-number class="inputs" allowClear v-model="formOption.formData.spec" :placeholder="tabName=='喷干-过筛'?'喷干重量(kg)':'混二重量(kg)'"></a-input-number>
                                       </a-form-model-item>
                                   </a-col>
                                   <a-col :span="6">
                                       <a-form-model-item :label="tabName=='喷干-过筛'?'喷干件数':'混二件数'" prop="spec">
                                           <a-input-number class="inputs" allowClear v-model="formOption.formData.spec" :placeholder="tabName=='喷干-过筛'?'喷干件数':'混二件数'"></a-input-number>
                                       </a-form-model-item>
                                   </a-col>
                               </a-row>
                           </div>
                       </a-card>
                       <a-card class="table" :title="cardTitle2">
                           <div class="tool-bar">
                               <a-row>
                                   <a-col :span="6">
                                       <a-form-model-item label="出库日期" prop="outPutDate">
                                           <a-date-picker
                                               class="inputs"
                                               allowClear
                                               v-model="formOption.formData.outPutDate"
                                               valueFormat="YYYY-MM-DD"
                                               placeholder="出库日期"
                                               :locale="locale" />
                                       </a-form-model-item>
                                   </a-col>
                                   <a-col :span="6">
                                       <a-form-model-item :label="tabName=='喷干-过筛'?'喷干重量(kg)':'制粒重量(kg)'" prop="PKweight">
                                           <a-input-number class="inputs" allowClear v-model="formOption.formData.PKweight" :placeholder="tabName=='喷干-过筛'?'喷干重量(kg)':'制粒重量(kg)'"></a-input-number>
                                       </a-form-model-item>
                                   </a-col>
                                   <a-col :span="6">
                                       <a-form-model-item :label="tabName=='喷干-过筛'?'过筛件数':'制粒件数'" prop="screenOutStorageNum">
                                           <a-input-number
                                               class="inputs"
                                               allowClear
                                               v-model="formOption.formData.screenOutStorageNum"
                                               :placeholder="tabName=='喷干-过筛'?'过筛件数':'制粒件数'"
                                           />
                                       </a-form-model-item>
                                   </a-col>

                                   <a-col :span="6">
                                       <a-form-model-item :label="tabName=='喷干-过筛'?'收粉结存(kg)':'混二结存(kg)'" prop="balance">
                                           <a-input-number
                                               class="inputs"
                                               allowClear
                                               v-model="formOption.formData.balance"
                                               :placeholder="tabName=='喷干-过筛'?'收粉结存(kg)':'混二结存(kg)'"
                                           />
                                       </a-form-model-item>
                                   </a-col>
                               </a-row>
                           </div>
                       </a-card>
                   </div>
               </a-form-model>
           </div>
           <div slot="footer" style="text-align: center">
               <a-button size="large" @click="modalOption.onCancel">取 消</a-button>
               <a-button size="large" type="primary" @click="modalOption.onOk">确 定</a-button>
           </div>
       </a-spin>
    </a-modal>
</template>
<script lang="ts">
import { Vue, Component, Prop, Watch } from "vue-property-decorator";
import locale from "ant-design-vue/lib/date-picker/locale/zh_CN";
import nextApi from "@/api/nextApi";
import areaJson, {getAreaWords} from "@/utils/area";

@Component({})
export default class extends Vue {
    @Prop({ default: false }) visible!: boolean;
    @Prop({ default: "" }) editId!: string;
    @Prop({ default: {} }) formData!: object;
    @Prop({default:''}) tabName!:string;
    @Watch("visible")
    visibleChange(val) {
        if (val) {
            this.checkTitleName()
        }else{
            //关闭弹窗初始化数据组
            this.searchSource = {
                materialNameList:[]
            };
            (this.$refs.formModel as any).resetFields()
        }
    }
    loading = false
    disabledEdit = false
    @Watch("editId",{immediate:true})
    editIdChange(val) {
        if (val) {
            console.log('id:',val)
            this.modalOption.title = `编辑`;
            this.disabledEdit = true
            this.getInfo()
        }else{
            this.modalOption.title = `新增`;
        }
    }
    cardTitle1 = "喷干入"
    cardTitle2 = "过筛领"
    //组件配置
    locale = locale;
    modalOption: any = {
        width: "90%",
        title: "新增",
        okText: "保存",
        cancelText: "取消",
        loading: false,
        onOk: () => {
            this.modalOk();
        },
        onCancel: () => {
            this.closeModal();
        },
    };
    //表单数据源
    searchSource:any = {
        materialNameList:[]
    }
    //表单配置
    formOption: any = {
        labelCol: { style: { width: "140px" } },
        wrapperCol: { style: {width:'calc(100% - 140px)'}},
        layout: "inline",
        labelAlign: "right",
        hideRequiredMark: false,
        formData: {},
        rules: {},
    };
    checkTitleName(){
        if(this.tabName == '喷干-过筛'){
            this.cardTitle1 = "喷干入"
            this.cardTitle2 = "过筛领"
        }else{
            this.cardTitle1 = "总混(二)入"
            this.cardTitle2 = "制粒领"
        }
    }
    materialTypeChange(v){
        console.log('change',v)
        if(v){
            this.getMaterialNameList(v)
        }
        if(v === '配方颗粒(颗粒)'){
            this.formOption.rules = {
                materialType: [{ required: true, message: "请选择库存类型", trigger: "blur" }],
                categoryCode: [{ required: true, message: "请选择标准类型", trigger: "blur" }],
                materialName: [{ required: true, message: "请选择品名名称", trigger: "blur" }],
                keLiBatch: [{ required: true, message: "请输入颗粒批号"}],
                stockNumSys: [{ required: true, message: "请输入颗粒结存量"}],
                qualityStatusKeLi: [{ required: true, message: "请选择质量(颗粒)状态"}],
                klProdTime: [{ required: true, message: "请选择复检期"}],
            }
        }else{
            this.formOption.rules = {
                materialType: [{ required: true, message: "请选择库存类型", trigger: "blur" }],
                categoryCode: [{ required: true, message: "请选择标准类型", trigger: "blur" }],
                materialName: [{ required: true, message: "请选择品名名称", trigger: "blur" }],
                fBatch: [{ required: true, message: "请输入粉批号"}],
                jgfType: [{ required: true, message: "请选择库存类型"}],
                qualityStatusOne: [{ required: true, message: "请选择质量状态"}],
                recheckTime: [{ required: true, message: "请选择复检期"}],
            }
        }
    }
    materialIdChange(v){
        let find = this.searchSource.materialNameList.find(item=>item.id === v)
        if(find){
            this.formOption.formData.materialId = find.id
        }
    }
    /**
     * @description 查询一些数据
     * **/
    async getMaterialNameList(type){
        this.loading = true
        const res = await nextApi.selectMaterialListByType({materialType:type})
        this.searchSource.materialNameList = res.data
        this.loading = false
    }
    //查询详情后进行修改
    async getInfo(){
        this.formOption.formData = this.formData
        await this.materialTypeChange(this.formOption.formData.materialType)
    }
    //提交
    modalOk(){
        (this.$refs.formModel as any).validate(async (valid: boolean) => {
            if (valid) {
                this.modalOption.loading = true;
                try {
                    // if(this.editId){
                    //     console.log(this.formOption.formData,'修改')
                    //     const res = await nextApi.productStorageUpdate(this.formOption.formData)
                    //     this.$message.success(res.msg)
                    //     this.$emit("closeModal")
                    // }else{
                    //     console.log(this.formOption.formData,'新增')
                    //     const res = await nextApi.rawProductStorageAdd(this.formOption.formData)
                    //     this.$message.success(res.msg)
                    //     this.$emit("closeModal")
                    // }
                }finally {
                    this.modalOption.loading = false;
                }
            }
        });
    }

    closeModal() {
        this.disabledEdit = false
        this.formOption.formData = {}
        this.$emit("closeModal");
    }
}
</script>
<style scoped lang="scss">
.container {
    padding: 0;
    .MR-10 {
        margin-right: 10px;
    }
    .inputs {
        width: 200px;
    }
    .inputs-with-qus{
        width: 173px;
    }
    .tool-bar {
        margin-bottom: 20px;

    }
    .table {
        margin-top: 20px;
    }
}
</style>
